<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    #box {
      width: 1100px;
      position: relative;
      margin: 0 auto;
    }

    .item {
      position: absolute;
      width: 200px;
      border: 1px solid greenyellow;
      padding: 5px;
    }

    img {
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="box">
    <div class="item">
      <img src="./image/1.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/2.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/3.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/4.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/5.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/6.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/7.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/8.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/9.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/10.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/11.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/12.jpg" alt="">
    </div>
    <div class="item">
      <img src="./image/13.jpg" alt="">
    </div>

  </div>
</body>

</html>
<script>
  var data = [
    { src: "./image/1.jpg" },
    { src: "./image/2.jpg" },
    { src: "./image/3.jpg" },
    { src: "./image/4.jpg" },
    { src: "./image/5.jpg" },
    { src: "./image/6.jpg" },
    { src: "./image/7.jpg" },
    { src: "./image/8.jpg" },
    { src: "./image/9.jpg" },
    { src: "./image/10.jpg" },
    { src: "./image/11.jpg" },
    { src: "./image/12.jpg" },
    { src: "./image/13.jpg" },
    { src: "./image/14.jpg" },
    { src: "./image/15.jpg" },
    { src: "./image/16.jpg" },
    { src: "./image/17.jpg" },
    { src: "./image/18.jpg" },
    { src: "./image/19.jpg" },
    { src: "./image/20.jpg" },
    { src: "./image/21.jpg" },
    { src: "./image/22.jpg" },
    { src: "./image/23.jpg" },
    { src: "./image/24.jpg" },
    { src: "./image/25.jpg" },
    { src: "./image/26.jpg" },
  ]
  window.onload = function () {
    // 1.获取外层盒子宽度 boxWidth
    var boxobj = document.getElementById('box') //获取外层盒子
    var boxWidth = boxobj.offsetWidth
    // 2.图片盒子宽度 itemWidth
    var itemobj = document.getElementsByClassName('item') //此时获得的为一个数组
    var itemWidth = itemobj[0].offsetWidth //offsetWidth是只读属性，且获得的宽度是整体宽度 包含边框和边距
    // 3.列数 = boxWidth / itemWidth
    var column = parseInt(boxWidth / itemWidth) // 计算得到一行可以有多少列
    var spacing = (boxWidth - column * itemWidth) / (column - 1) //计算间距 用于布局时候 item的left的值


    //存储 每一列的长度 并且把 最短的一列的长度和Index暴露出来
    var arr = [] // 用于存储每一列的长度
    var obj = {}

    function getMinLength() {
      obj.minLength = arr[0]
      obj.minIndex = 0
      for (var j = 0; j < arr.length; j++) {
        if (arr[j] <= obj.minLength) {
          obj.minLength = arr[j]
          obj.minIndex = j
        }
      }
    }
    function layOut() {
      for (var i = 0; i < itemobj.length; i++) { //循环遍历图片盒子数组
        if (i < column) { // 第一行的布局
          itemobj[i].style.top = '0px'
          itemobj[i].style.left = (itemWidth + spacing) * i + 'px'
          arr[i] = itemobj[i].offsetHeight// 把第一行每一列的高度存入数组
        } else {
          getMinLength()
          itemobj[i].style.left = (obj.minIndex * (itemWidth + spacing)) + 'px' //设置left
          itemobj[i].style.top = 25 + obj.minLength + 'px'//设置top， 考虑上面已有相片
          arr[obj.minIndex] += itemobj[i].offsetHeight//更新数组对应列的长度
        }
      }
    }
    layOut()
    //懒加载函数   监听页面滚动
    window.onscroll = function () {
      if ((window.scrollY + window.innerHeight) > obj.minLength) {
        for (var k = 0; k < data.length; k++) {
          // var img = document.createElement('img')//创建图片标签
          // img.src = data[k].src
          var imgbox = document.createElement('div')
          imgbox.className = "item"
          imgbox.innerHTML = "<img src=" + data[k].src + " alt=''>"
          boxobj.appendChild(imgbox)
        }
        layOut()
      }
    }


  }
</script>